#!/usr/bin/env python
# encoding: utf-8

from django.db import models
from django.contrib.sites.models import Site
from datetime import datetime
from django.contrib.auth.models import User
from django.conf import settings
from django.contrib.sites.models import Site
from django.contrib.sites.managers import CurrentSiteManager
from django.template.defaultfilters import slugify
import os
import hashlib
from ponyfolio import settings as pony_settings 

"""
TODO: gig categories
TODO: django tagging
"""

class Gig(models.Model):
    url = models.URLField("URL", blank=True, null=True)
    title = models.CharField("Title", max_length=128)
    text = models.TextField("Description", blank=True, null=True, help_text="HTML")
    case = models.TextField("Case study", blank=True, null=True, help_text="HTML")
    active = models.BooleanField("Visible?", default=True)
    date_added = models.DateTimeField("Date added", blank=True, auto_now_add = True)
    ordering = models.IntegerField("Ordering", blank=True, null=True)
    sites = models.ManyToManyField(Site, verbose_name="Sites")
    on_site = CurrentSiteManager('sites')
    
    class Meta:
        verbose_name = "Gig"
        verbose_name_plural = "Gigs"
        get_latest_by = ('ordering', 'date_added', 'id',)
        ordering = ('-ordering', '-date_added', '-id',)
        
    def __unicode__(self):
        return u"%s " % self.title
    
    
class Screenshot(models.Model):
    def upload_path(self, field_attname):
        fname, extension = os.path.splitext(field_attname)
        fname = slugify(fname[:32])
        filename = "".join([fname, '_', hashlib.md5(field_attname).hexdigest()[:4], extension])
        return "uploads/%s/screenshots/%s" % (Site.objects.get_current().name, filename)  
    gig = models.ForeignKey(Gig, verbose_name="Gig")
    title = models.CharField("Title", max_length=32, null=True, blank=True)
    file = models.ImageField(upload_to=upload_path, verbose_name="Image", help_text="Will be resized. See template gig.html")
    date_added = models.DateTimeField("Date added", blank=True, auto_now_add = True)
    active = models.BooleanField("Visible?", default=True)
    
    class Meta:
        verbose_name = "Screenshot"
        verbose_name_plural = "Screenshots"
        get_latest_by = ('date_added', 'id',)
        ordering = ('-date_added', '-id',)
        
    def __unicode__(self):
        return u"%s " % self.title


class SiteMeta(models.Model):
    title = models.CharField("Title", max_length=255)
    tagline = models.CharField("Tagline", max_length=255, blank=True, null=True)
    author = models.CharField("Meta author", max_length=64, blank=True, null=True)
    copyright = models.CharField("Meta copyrights", max_length=64, blank=True, null=True)
    description = models.CharField("Meta description", max_length=255, blank=True, null=True)
    keywords = models.CharField("Meta keywords", max_length=255, blank=True, null=True)
    robots = models.CharField("Meta author", max_length=64, blank=True, null=True, default="index, nofollow")
    footer = models.TextField("Footer", blank=True, null=True, help_text="HTML")
    
    site = models.OneToOneField(Site, verbose_name="Site")
    on_site = CurrentSiteManager('site')
    
    class Meta:
        verbose_name = "Meta data"
        verbose_name_plural = "Meta data"
        get_latest_by = ('id')
        
    def __unicode__(self):
        return u"%s " % self.title
    

class ContactInfo(models.Model):
    email = models.EmailField("Email", blank=True, null=True)
    phone = models.CharField("Phone", max_length=128, blank=True, null=True)
    skype = models.CharField("Skype ID", max_length=128, blank=True, null=True)
    facebook = models.URLField("Facebook URL", blank=True, null=True)
    stackoverflow = models.URLField("StackOverflow URL", blank=True, null=True)
    djangopeople = models.URLField("DjangoPeople URL", blank=True, null=True)
    whatever = models.TextField("Whatever else", blank=True, null=True, help_text="HTML")
    
    site = models.OneToOneField(Site, verbose_name="Site")
    on_site = CurrentSiteManager('site')
    
    class Meta:
        verbose_name = "Contact info"
        verbose_name_plural = "Contact info"
        get_latest_by = ('id')

    
    